package com.hj.security.ui;

import com.hj.security.R;
import com.hj.security.utils.MD5Encoder;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class LostProtectedActivity extends Activity implements OnClickListener {

	private SharedPreferences sp;
	private Dialog dialog;
	private EditText password;
	private EditText confirmPassword;
	private TextView tv_lost_protected_number;
	private CheckBox cb_lost_protected_isProtected;
	private TextView tv_lost_protected_guide;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		sp = getSharedPreferences("config", Context.MODE_PRIVATE);
		if(isSetPassword()){
			showLoginDialog();
		}else{
			showFirstDialog();
		}
	}
	private void showFirstDialog() {
		dialog = new Dialog(this, R.style.MyDialog);
		View view = View.inflate(this, R.layout.first_dialog, null);
		password = (EditText) view.findViewById(R.id.et_protected_first_password);
		confirmPassword = (EditText) view.findViewById(R.id.et_protected_confirm_password);
		Button yes = (Button) view.findViewById(R.id.bt_protected_first_yes);
		Button no = (Button) view.findViewById(R.id.bt_protected_first_no);
		yes.setOnClickListener(this);
		no.setOnClickListener(this);
		dialog.setContentView(view);
		dialog.setCancelable(false);
		dialog.show();
		
	}
	private void showLoginDialog() {
		dialog = new Dialog(this, R.style.MyDialog);
		View view = View.inflate(this, R.layout.login_dialog, null);
		password = (EditText) view.findViewById(R.id.et_protected_password);
		Button yes = (Button) view.findViewById(R.id.bt_protected_login_yes);
		Button no = (Button) view.findViewById(R.id.bt_protected_login_no);
		yes.setOnClickListener(this);
		no.setOnClickListener(this);
		dialog.setContentView(view);
		dialog.setCancelable(false);
		dialog.show();		
		
	}
	private boolean isSetPassword(){
		String password = sp.getString("password", "");
		if("".equals(password)){
			return false;
		}else{
			return true;
		}
	}
	private boolean isSetupGuide(){
		//return sp.getBoolean("setupGuide", false);
		return true;
	}
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.bt_protected_first_yes:
			String fp = password.getText().toString().trim();
			String cp = confirmPassword.getText().toString().trim();
			if ("".equals(fp) || "".equals(cp)) {
				Toast.makeText(this, "密码不能为空", Toast.LENGTH_SHORT).show();
				return;
			} else {
				if (fp.equals(cp)) {
					Editor editor = sp.edit();
					editor.putString("password", MD5Encoder.encode(fp));
					editor.commit();
					dialog.dismiss();
					if(!isSetupGuide()){
						finish();
						Intent intent = new Intent(this, SetupGuide1Activity.class);
						startActivity(intent);
					}
					finish();
				} else {
					Toast.makeText(this, "两次输入的密码不一致，请重新输入", Toast.LENGTH_SHORT).show();
					return;
				}
			}
			dialog.dismiss();
			break;
		case R.id.bt_protected_first_no:
			dialog.dismiss();
			finish();			
			break;
		case R.id.bt_protected_login_yes:
			String pwd = password.getText().toString().trim();
			if("".equals(pwd)){
				Toast.makeText(this, "请输入密码", Toast.LENGTH_LONG).show();
			}else{
				String str = sp.getString("password", "");
				if(MD5Encoder.encode(pwd).equals(str)){
					
					if(isSetupGuide()){
						setContentView(R.layout.lost_protected);
						tv_lost_protected_guide = (TextView) findViewById(R.id.tv_lost_protected_guide);
						tv_lost_protected_number = (TextView) findViewById(R.id.tv_lost_protected_number);
						cb_lost_protected_isProtected = (CheckBox) findViewById(R.id.cb_lost_protected_isProtected);
						
						tv_lost_protected_number.setText("手机安全号码为："+sp.getString("number", ""));
						tv_lost_protected_guide.setOnClickListener(this);
						
						boolean isProtecting = sp.getBoolean("isProtected", false);
						if(isProtecting){
							cb_lost_protected_isProtected.setText("已经开启保护");
							cb_lost_protected_isProtected.setChecked(true);
						}
						cb_lost_protected_isProtected.setOnCheckedChangeListener(new OnCheckedChangeListener() {
							
							public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
								if(isChecked){
									cb_lost_protected_isProtected.setText("已经开启保护");
									Editor editor = sp.edit();
									editor.putBoolean("isProtected", true);
									editor.commit();
								}else{
									cb_lost_protected_isProtected.setText("没有开启保护");
									Editor editor = sp.edit();
									editor.putBoolean("isProtected", false);
								}
							}
						});
						
					}
					
					dialog.dismiss();
					//finish();
					//Intent intent = new Intent(this, SetupGuide1Activity.class);
					//startActivity(intent);
				}else{
					Toast.makeText(this, "密码错误", Toast.LENGTH_LONG).show();
				}
			}
			break;
		case R.id.bt_protected_login_no:
			dialog.dismiss();
			finish();			
			break;
		case R.id.tv_lost_protected_guide:
			finish();
			Intent setupGuideIntent = new Intent(this, SetupGuide1Activity.class);
			startActivity(setupGuideIntent);
			break;

		default:
			break;
		}
		
	}
}
